﻿<UserControl x:Class="PasswordProvider.View.Library.EnumSelector"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:PasswordProvider.View.Library"
    x:Name="this"
    >
    <UserControl.Resources>

        <DataTemplate DataType="{x:Type src:EnumSelectorDisplayItem}">
            <TextBlock Text="{Binding DisplayName}" ToolTip="{Binding ToolTip}" />
        </DataTemplate>

        <ControlTemplate x:Key="flagsLBItemTemplate" TargetType="{x:Type ListBoxItem}">
            <CheckBox IsHitTestVisible="true"
                 Focusable="false"
                 Foreground="{TemplateBinding Foreground}"
                 BorderBrush="{Binding ElementName=this, Path=Foreground}"
                 IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                 Content="{TemplateBinding Content}"
                      />

        </ControlTemplate>

        <ControlTemplate x:Key="noFlagsLBItemTemplate" TargetType="{x:Type ListBoxItem}">
            <Border Background="Transparent">
                <RadioButton IsHitTestVisible="true"
                     Focusable="false"
                     Foreground="{TemplateBinding Foreground}"
                     BorderBrush="{Binding ElementName=this, Path=Foreground}"
                     Content="{TemplateBinding Content}"
                     IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
            </Border>
        </ControlTemplate>

        <ControlTemplate x:Key="ListBoxTemplate">
            <ListBox x:Name="SelectorObject"
                     BorderBrush="Transparent"
                    ItemsSource="{Binding ElementName=this, Path=DisplayItems}"
                    KeyboardNavigation.DirectionalNavigation="Cycle"
                    Background="{Binding ElementName=this, Path=Background}"
                    Foreground="{Binding ElementName=this, Path=Foreground}"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             >
                <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=this, Path=TreatAsFlags}" Value="False">
                                <Setter Property="Template" Value="{StaticResource noFlagsLBItemTemplate}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=this, Path=TreatAsFlags}" Value="True">
                                <Setter Property="Template" Value="{StaticResource flagsLBItemTemplate}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
        </ControlTemplate>
        
        <ControlTemplate x:Key="ComboBoxTemplate">
            <src:MultiComboBox  x:Name="SelectorObject"
                                SelectionMode="Single"
                                ItemsSource="{Binding ElementName=this, Path=DisplayItems}"
                                KeyboardNavigation.DirectionalNavigation="Cycle"
                                ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
        </ControlTemplate>


    </UserControl.Resources>

    <UserControl.Style>
        <Style TargetType="{x:Type src:EnumSelector}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=this, Path=Mode}" Value="ListBox">
                    <Setter Property="Template" Value="{StaticResource ListBoxTemplate}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=this, Path=Mode}" Value="ComboBox">
                    <Setter Property="Template" Value="{StaticResource ComboBoxTemplate}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>

</UserControl>
